﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHTU;
using NOMFinalSettlement.DatasetReport;
using NOMFinalSettlement.CrystalReport;
namespace NOMFinalSettlement.AppForm.Program
{
    public partial class fReportNotFinish : DevComponents.DotNetBar.Office2007Form
    {
        FinalSettlementModel.TableHoSo profile = null;
        FinalSettlementModel.TableCanBo officer = null;

        public fReportNotFinish()
        {
            InitializeComponent();
        }

        private void btnProcessing_Click(object sender, EventArgs e)
        {
            if (bgw.IsBusy != true)
            {
                bgw.RunWorkerAsync();
            }
        }

        private void timerLoading_Tick(object sender, EventArgs e)
        {
            circularProgress.Value += 2;
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            EnableCircular(true);
            btnProcessing.Enabled = false;
            lbStatus.Text = " Đang xử lý ...";
            doSearch();
        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EnableCircular(false);
            btnProcessing.Enabled = true;
            lbStatus.Text = "";
        }
        void EnableCircular(bool enable)
        {
            circularProgress.Visible = enable;
            circularProgress.IsRunning = enable;
            if (enable)
            {
                timerLoading.Start();
            }
            else
            {
                timerLoading.Stop();
            }
        }

        //Xử lý
        void doSearch()
        {
            DateTime _dateStart = DateTime.Now;
            dsReportNotFinish reportNotFinishds = new dsReportNotFinish();
            var tblProfile = profile.listProfile().Where(w => w.HoanThanhQuyetToan == "Chưa hoàn thành");
            var tblOfficer = officer.listOfficer();
            if (dateStart.Text != "")
            {
                _dateStart = dateStart.Value;
                tblProfile = tblProfile.Where(w => w.NgayNhan == _dateStart);
            }
            int stt = 1;
            foreach (var rowOfficer in tblOfficer)
            {
                var tblProfile_Officer = tblProfile.Where(w => w.CanBo == rowOfficer.CanBo);

                DataRow row = reportNotFinishds.dtReportNotFinish.NewRow();
                row["colSTT"] = stt;
                row["colCanBoThamTra"] = rowOfficer.CanBo;
                row["colHoSoChuaHoanThanh"] = tblProfile_Officer.Count();
                if (tblProfile_Officer.Count() > 0)
                {
                    row["colDeNghiQuyetToan"] = String.Format("{0:0,0.00}", tblProfile_Officer.Sum(s => s.DeNghiGiaTriQuyetToan));
                    row["colHoSoQuaHan"] = tblProfile_Officer.Count(c => c.NgayNhan > _dateStart.AddDays(90));
                    row["colHoSoTrinhKy"] = tblProfile_Officer.Count(c => c.TrinhKy == "Đã trình ký" && c.HoanThanhQuyetToan == "Chưa hoàn thành");
                }
                else
                {
                    row["colDeNghiQuyetToan"] = "";
                    row["colHoSoQuaHan"] = "";
                    row["colHoSoTrinhKy"] = "";
                }

                row["totalHoSoChuaHoanThanhQT"] = tblProfile.Count();
                row["totalHoSoQuaHan"] = tblProfile.Count(c => c.NgayNhan > _dateStart.AddDays(90));
                reportNotFinishds.dtReportNotFinish.Rows.Add(row);
                stt++;
            }
            cReportNotFinish reportNotFinishCr = new cReportNotFinish();
            reportNotFinishCr.SetDataSource(reportNotFinishds);
            Invoke(new MethodInvoker(() => crystalReportViewer1.ReportSource = reportNotFinishCr), null);
            reportNotFinishCr.SetParameterValue("dateReport", _dateStart.ToShortDateString());            
        }

        string IsText(Control c)
        {
            var _self = MainApp.Self.appProcess;
            return _self.crossText(c);
        }

        private void fReportNotFinish_Load(object sender, EventArgs e)
        {
            profile = new FinalSettlementModel.TableHoSo();
            officer = new FinalSettlementModel.TableCanBo();
        }
    }
}
